<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>存储类说明符</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/storage_duration","wgTitle":"cpp/language/storage duration","wgCurRevisionId":72719,"wgArticleId":2754,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/storage_duration","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_storage_duration skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">存储类说明符</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="cpp/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations">声明语法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constexpr.html" title="cpp/language/constexpr"><span class="t-lines"><span>constexpr</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 存储类说明符</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="tu_local.html" title="cpp/language/tu local">翻译单元局部</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference.html" title="cpp/language/reference"> 引用</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 简单声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 链接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 属性声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 空声明</a> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>存储类说明符是一个名字的<a href="declarations.html" title="cpp/language/declarations">声明语法</a>的<span class="t-spar">声明说明符序列</span>的一部分。它与名字的<a href="scope.html" title="cpp/language/scope">作用域</a>一同控制名字的两个独立性质：其“存储期”与其“链接”。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx11"><td>
<dl><dd><ul><li><code><b>auto</b></code> - <i>自动</i>存储期。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></td></tr>
<tr class="t-rev t-until-cxx17"><td>
<dl><dd><ul><li><code><b>register</b></code> - <i>自动</i>存储期，另提示编译器将此对象置于处理器的寄存器。<span class="t-mark">(弃用)</span>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></td></tr>
</table>
<dl><dd><ul><li><code><b>static</b></code> - <i>静态</i>或<i>线程</i>存储期和<i>内部</i>链接。
</li><li><code><b>extern</b></code> - <i>静态</i>或<i>线程</i>存储期和<i>外部</i>链接。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<dl><dd><ul><li><code><b>thread_local</b></code> - <i>线程</i>存储期。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<dl><dd><ul><li><code><b>mutable</b></code> - 不影响存储期或链接。解释见 <a href="cv.html" title="cpp/language/cv"> const/volatile</a> 。
</li></ul>
</dd></dl>
<p><br>
声明中只可以出现一个存储类说明符<span class="t-rev-inl t-since-cxx11"><span>，但 <code>thread_local</code> 可以与 <code>static</code> 或 <code>extern</code> 结合</span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>。
</p>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx11"><td>
<div class="t-li1"><span class="t-li">1)</span> <code>auto</code> 说明符只能搭配声明于块作用域或函数形参列表中的对象。它指示自动存储期，即这种声明的默认情况。此关键词的含义已于 C++11 更改。
</div></td>
<td><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></td></tr>
<tr class="t-rev t-until-cxx17"><td>
<div class="t-li1"><span class="t-li">2)</span> <code>register</code> 说明符只能搭配声明于块作用域或函数形参列表中的对象。它指示自动存储期，即这种声明的默认情况。另外，此关键词的存在可用于提示优化器将此变量的值存储于 CPU 寄存器。此关键词已于 C++11 被弃用。
</div></td>
<td><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></td></tr>
</table>
<div class="t-li1"><span class="t-li">3)</span> <code>static</code> 说明符只能搭配（函数形参列表外的）对象声明、（块作用域外的）函数声明及匿名联合体声明。当用于声明类成员时，它会声明一个<a href="static.html" title="cpp/language/static">静态成员</a>。当用于声明对象时，它指定静态存储期（除非与 <code>thread_local</code> 协同出现）。在命名空间作用域内声明时，它指定内部链接。</div>
<div class="t-li1"><span class="t-li">4)</span> <code>extern</code> 声明符只能搭配变量声明和函数声明（除了类成员或函数形参）。它指定外部链接，而且技术上不影响存储期，但它不能用来定义自动存储期的对象，故所有 <code>extern</code> 对象都具有静态或线程存储期。另外，使用 <code>extern</code> 且没有初始化器的声明不是<a href="definition.html" title="cpp/language/definition">定义</a>。</div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<div class="t-li1"><span class="t-li">5)</span> <code>thread_local</code> 关键词只能搭配声明于命名空间作用域的对象、声明于块作用域的对象及静态数据成员。它指示对象具有线程存储期。它能与 <code>static</code> 或 <code>extern</code> 结合，以分别指定内部或外部链接（但静态数据成员始终拥有外部链接），但额外的 <code>static</code> 不影响存储期。</div>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<h4><span class="mw-headline" id=".E5.AD.98.E5.82.A8.E6.9C.9F">存储期</span></h4>
<p>程序中的所有<a href="object.html" title="cpp/language/object">对象</a>都具有下列存储期之一：
</p>
<dl><dd><ul><li><i><b>自动（automatic）</b></i>存储期。这类对象的存储在外围代码块开始时分配，并在结束时解分配。未声明为 <code>static</code>、<code>extern</code> 或 <code>thread_local</code> 的所有局部对象均拥有此存储期。
</li></ul>
</dd></dl>
<dl><dd><ul><li><i><b>静态（static）</b></i>存储期。这类对象的存储在程序开始时分配，并在程序结束时解分配。这类对象只存在一个实例。所有声明于命名空间（包含全局命名空间）作用域的对象，加上声明带有 <code>static</code> 或 <code>extern</code> 的对象均拥有此存储期。有关拥有此存储期的对象的初始化的细节，见<a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">非局部变量</a>与<a href="storage_duration.html#.E9.9D.99.E6.80.81.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F">静态局部变量</a>。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<dl><dd><ul><li><i><b>线程（thread）</b></i>存储期。这类对象的存储在线程开始时分配，并在线程结束时解分配。每个线程拥有其自身的对象实例。只有声明为 <code>thread_local</code> 的对象拥有此存储期。 <code>thread_local</code> 能与 <code>static</code> 或 <code>extern</code> 一同出现，它们用于调整链接。关于具有此存储期的对象的初始化的细节，见<a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">非局部变量</a>和<a href="storage_duration.html#.E9.9D.99.E6.80.81.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F">静态局部变量</a>。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<dl><dd><ul><li><i><b>动态（dynamic）</b></i>存储期。这类对象的存储是通过使用<a href="../memory.html" title="cpp/memory">动态内存分配</a>函数来按请求进行分配和解分配的。关于具有此存储期的对象的初始化的细节，见 <a href="new.html" title="cpp/language/new">new 表达式</a>。
</li></ul>
</dd></dl>
<h4><span class="mw-headline" id=".E9.93.BE.E6.8E.A5">链接</span></h4>
<p>指代对象、引用、函数、类型、模板、命名空间或值的名字，可具有<i>链接</i>。如果某个名字具有链接，那么它所指代的实体与另一作用域中的声明所引入的相同名字指代相同的实体。如果有变量、函数或其他实体声明于数个作用域但没有足够的链接，则将生成该实体的多个实例。
</p><p>以下各种链接可以被识别：
</p>
<h5><span class="mw-headline" id=".E6.97.A0.E9.93.BE.E6.8E.A5">无链接</span></h5>
<p>名字只能从其所在的作用域使用。
声明于块作用域的下列任何名字均无链接：
</p>
<ul><li> 未显式声明为 <code>extern</code> 的变量（不管有没有 <code>static</code> 修饰符）；
</li><li> <a href="class.html#.E5.B1.80.E9.83.A8.E7.B1.BB" title="cpp/language/class">局部类</a>及其成员函数；
</li><li> 声明于块作用域的其他名字，例如 typedef、枚举及枚举项。
</li></ul>
<p>未指定为拥有外部<span class="t-rev-inl t-since-cxx20"><span>、模块</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>或内部链接的名字同样无链接，这与其声明所处的作用域无关。
</p>
<h5><span class="mw-headline" id=".E5.86.85.E9.83.A8.E9.93.BE.E6.8E.A5">内部链接</span></h5>
<p>名字可从当前翻译单元中的所有作用域使用。
声明于命名空间作用域的下列任何名字均具有内部链接；
</p>
<ul><li> 声明为 <code>static</code> 的变量<span class="t-rev-inl t-since-cxx14"><span>、变量模板</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>、函数或函数模板；
</li><li> 未声明为 <code>extern</code> 且先前未声明为具有外部链接的非 volatile <span class="t-rev-inl t-since-cxx14"><span>非模板</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span><span class="t-rev-inl t-since-cxx17"><span>非 inline</span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span><span class="t-rev-inl t-since-cxx20"><span>且未被导出</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>的 <a href="cv.html" title="cpp/language/cv">const 限定的</a>变量（包含 <a href="constexpr.html" title="cpp/language/constexpr">constexpr</a>）；
</li><li> <a href="union.html" title="cpp/language/union">匿名联合体</a>的数据成员。
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<p>另外，所有声明于<a href="namespace.html" title="cpp/language/namespace">无名命名空间</a>或无名命名空间内的命名空间中的名字，即使显式声明为 <code>extern</code>，也均拥有内部链接。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<h5><span class="mw-headline" id=".E5.A4.96.E9.83.A8.E9.93.BE.E6.8E.A5">外部链接</span></h5>
<p>名字能从其他翻译单元中的作用域使用。具有外部链接的变量和函数也具有<a href="language_linkage.html" title="cpp/language/language linkage">语言链接</a>，这使得以不同编程语言编写的翻译单元可以互相链接。
声明于命名空间作用域的下列任何名字均具有外部链接，除非这些名字在无名命名空间内声明<span class="t-rev-inl t-since-cxx20"><span>或它们声明于具名模块且未被导出</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>：
</p>
<ul><li> 以上未列出的变量与函数（即未声明为 <code>static</code> 的函数、命名空间作用域内未声明为 <code>static</code> 的非 const 变量，和所有声明为 <code>extern</code> 的变量）；
</li><li> 枚举；
</li><li> 类以及其成员函数、静态数据成员（不论是否 const）、嵌套类及枚举，及首次以类体内的 <a href="friend.html" title="cpp/language/friend">friend</a> 声明引入的函数的名字；
</li><li> 所有未列于上的模板名（即未声明为 <code>static</code> 的函数模板）。
</li></ul>
<p>任何首次声明于块作用域的下列名称拥有外部链接：
</p>
<ul><li> 声明为 <code>extern</code> 的变量名；
</li><li> 函数名。
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<h5> <span class="mw-headline" id=".E6.A8.A1.E5.9D.97.E9.93.BE.E6.8E.A5">模块链接</span></h5>
<p>名字只能从同一模块单元或同一具名模块中的其他翻译单元的作用域指代。
</p><p>如果声明于命名空间作用域的名字附着到具名模块，未被导出且无内部链接，则其拥有模块链接。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E9.9D.99.E6.80.81.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F">静态局部变量</span></h3>
<p>声明于块作用域且带有 <code>static</code><span class="t-rev-inl t-since-cxx11"><span> 或 <code>thread_local</code></span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span> 说明符的变量拥有静态<span class="t-rev-inl t-since-cxx11"><span>或线程</span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>存储期，但在控制首次经过其声明时才会被初始化（除非其初始化是<a href="zero_initialization.html" title="cpp/language/zero initialization">零初始化</a>或<a href="constant_initialization.html" title="cpp/language/constant initialization">常量初始化</a>，这可以在首次进入块前进行）。在其后所有的调用中，声明均被跳过。
</p><p>如果初始化<a href="throw.html" title="cpp/language/throw">抛出异常</a>，则不认为变量被初始化，且控制下次经过该声明时将再次尝试初始化。
</p><p>如果初始化递归地进入正在初始化的变量的块，则行为未定义。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<p>如果多个线程试图同时初始化同一静态局部变量，则初始化严格发生一次（类似的行为也可对任意函数以 <span class="t-lc"><a href="../thread/call_once.html" title="cpp/thread/call once">std::call_once</a></span> 来达成）。
</p><p>注意：此功能特性的通常实现均使用双检查锁定模式的变体，这使得对已初始化的局部静态变量检查的运行时开销减少为单次非原子的布尔比较。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<p>块作用域静态变量的析构函数在初始化已成功的情况下<a href="../utility/program/exit.html" title="cpp/utility/program/exit">在程序退出时</a>被调用。
</p><p>在相同<a href="inline.html" title="cpp/language/inline">内联函数</a>（可以是隐式内联）的所有定义中，函数局部的静态对象均指代定义于一个翻译单元中的同一对象，只要函数拥有外部链接。
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p>位于顶层命名空间作用域（C 中的文件作用域），且是 <code>const</code> 而非 <code>extern</code> 的名字在 C 中具有外部链接，但在 C++ 中具有内部链接。
</p><p>C++11 起，<code>auto</code> 不再是存储类说明符；它被用于指示类型推导。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx17"><td>
<p>在 C 中，不能取 <code>register</code> 变量的地址，但在 C++ 中，声明为 <code>register</code> 的对象与声明不带任何存储类说明符的变量在语义上没有区别。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></td></tr>
<tr class="t-rev t-since-cxx17"><td>
<p>不同于 C，在 C++ 中不能将变量声明为 <code>register</code>。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<p>从不同作用域指代的且带内部或外部链接的 <code>thread_local</code> 变量的名字可能指代相同或不同的实例，这取决于代码执行于相同还是不同的线程。
</p><p><code>extern</code> 关键词也能用于指定<a href="language_linkage.html" title="cpp/language/language linkage">语言链接</a>和<a href="class_template.html" title="cpp/language/class template">显式模板实例化声明</a>，但它在这些情况中不是存储类说明符（但当声明直接在语言链接说明中所包含时将声明当做如同它含 <code>extern</code> 说明符）。
</p><p><a href="cv.html" title="cpp/language/cv">关键词 <code>mutable</code></a> 在 C++ 语言的文法中是存储类说明符，尽管它并不影响存储期或链接。
</p><p><code>thread_local</code> 以外的存储类说明符都不能<a href="template_specialization.html" title="cpp/language/template specialization">显式特化</a>及<a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template">显式实例化</a>中使用：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> <span class="kw1">struct</span> S <span class="br0">{</span>
    thread_local <span class="kw4">static</span> <span class="kw4">int</span> tlm<span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span> <span class="sy1">&lt;&gt;</span> thread_local <span class="kw4">int</span> S<span class="sy1">&lt;</span><span class="kw4">float</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">tlm</span> <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="co1">// "static" 不出现于此</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.85.B3.E9.94.AE.E8.AF.8D">关键词</span></h3>
<p><a href="../keyword/auto.html" title="cpp/keyword/auto"><tt>auto</tt></a>,
<a href="../keyword/register.html" title="cpp/keyword/register"><tt>register</tt></a>,
<a href="../keywords/static.html" title="cpp/keyword/static"><tt>static</tt></a>,
<a href="../keyword/extern.html" title="cpp/keyword/extern"><tt>extern</tt></a>,
<a href="../keyword/thread_local.html" title="cpp/keyword/thread local"><tt>thread_local</tt></a>,
<a href="../keyword/mutable.html" title="cpp/keyword/mutable"><tt>mutable</tt></a>
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
<span class="co2">#include &lt;thread&gt;</span>
<span class="co2">#include &lt;mutex&gt;</span>
 
thread_local <span class="kw4">unsigned</span> <span class="kw4">int</span> rage <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> 
<a href="../thread/mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a> cout_mutex<span class="sy4">;</span>
 
<span class="kw4">void</span> increase_rage<span class="br0">(</span><span class="kw4">const</span> <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> thread_name<span class="br0">)</span>
<span class="br0">{</span>
    <span class="sy2">++</span>rage<span class="sy4">;</span> <span class="co1">// 在锁外修改 OK；这是线程局部变量</span>
    <a href="../thread/lock_guard.html"><span class="kw2166">std::<span class="me2">lock_guard</span></span></a><span class="sy1">&lt;</span><a href="../thread/mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a><span class="sy1">&gt;</span> lock<span class="br0">(</span>cout_mutex<span class="br0">)</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> thread_name <span class="sy1">&lt;&lt;</span> <span class="st0">" 的愤怒计数："</span> <span class="sy1">&lt;&lt;</span> rage <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../thread/thread.html"><span class="kw2146">std::<span class="me2">thread</span></span></a> a<span class="br0">(</span>increase_rage, <span class="st0">"a"</span><span class="br0">)</span>, b<span class="br0">(</span>increase_rage, <span class="st0">"b"</span><span class="br0">)</span><span class="sy4">;</span>
 
    <span class="br0">{</span>
        <a href="../thread/lock_guard.html"><span class="kw2166">std::<span class="me2">lock_guard</span></span></a><span class="sy1">&lt;</span><a href="../thread/mutex.html"><span class="kw2154">std::<span class="me2">mutex</span></span></a><span class="sy1">&gt;</span> lock<span class="br0">(</span>cout_mutex<span class="br0">)</span><span class="sy4">;</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"main 的愤怒计数："</span> <span class="sy1">&lt;&lt;</span> rage <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span>
 
    a.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
    b.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">a 的愤怒计数：2
main 的愤怒计数：1
b 的愤怒计数：2</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2387">CWG 2387</a>
</td>
<td> C++14
</td>
<td> 不明确 const 限定的变量模板是否默认有内部链接
</td>
<td> const 限定符不影响变量模板或其实例的链接
</td></tr></table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 3556/1000000
Preprocessor generated node count: 8764/1000000
Post‐expand include size: 83343/2097152 bytes
Template argument size: 18352/2097152 bytes
Highest expansion depth: 15/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:2754-0!*!0!!zh!*!zh!* and timestamp 20211125193147 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/storage_duration&amp;oldid=72719">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/storage_duration&amp;oldid=72719</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/storage_duration">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年10月13日 (星期三) 07:09。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过44,422次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.620 secs. -->
	</body>
<!-- Cached 20211125193147 -->
</html>